﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenSearch;

namespace OpenSearchTests
{
    /// <summary>
    ///This is a test class for RequestFactoryTest and is intended
    ///to contain all RequestFactoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class RequestFactoryTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes

        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //

        #endregion Additional test attributes

        /// <summary>
        ///A test for RequestFactory Constructor
        ///</summary>
        [TestMethod()]
        [DeploymentItem("simple_osdd.xml")]
        public void RequestFactoryConstructorTest()
        {
            RequestFactory target = new RequestFactory("simple_osdd.xml");
            Assert.IsNotNull(target);
        }

        /// <summary>
        ///A test for GetBestTemplateMatch
        ///</summary>
        [TestMethod()]
        [DeploymentItem("ddf_os.xml")]
        public void GetBestTemplateMatchTest()
        {
            RequestFactory target = new RequestFactory("ddf_os.xml");
            target.SubstituteSearchTermInTemplate("Talisman");

            Uri instantiatedTemplate = target.GetBestTemplateMatch();
            Assert.AreEqual(new Uri("http://192.168.40.180:8181/services/catalog/query?q=Talisman&format=atom"), instantiatedTemplate);
        }

        [TestMethod()]
        [DeploymentItem("q_talisman_format_atom.xml")]
        public void ResponseParserTest()
        {
            using (System.IO.FileStream responseStream = new System.IO.FileStream("q_talisman_format_atom.xml", System.IO.FileMode.Open))
            {
                Response response = new Response(responseStream);

                Assert.IsNotNull(response);

                Assert.IsNotNull(response.Title);
                Assert.AreEqual("Query Response", response.Title);

                Assert.AreEqual("ddf.distribution", response.Generator);

                Assert.AreEqual(1, response.Authors.Count);
                Assert.AreEqual("Codice Foundation", response.Authors[0].Name);

                Assert.AreEqual("urn:uuid:c9a4d9e7-d5eb-4298-84d2-2059628a3c10", response.Id);

                Assert.AreEqual(2, response.TotalResponseCount);
                Assert.AreEqual(1, response.StartIndex);
                Assert.AreEqual(10, response.ItemsPerPage);

                IEnumerable<Query> queries = response.Queries;
                Assert.AreEqual(0, queries.Count());

                Assert.AreEqual("", response.Description);

                IEnumerable<System.ServiceModel.Syndication.SyndicationItem> syndicationItems = response.ItemsRaw;
                List<System.ServiceModel.Syndication.SyndicationItem> listOfSyndicationItems = syndicationItems.ToList();
                Assert.AreEqual(2, listOfSyndicationItems.Count);

                System.ServiceModel.Syndication.SyndicationItem item0 = listOfSyndicationItems[0];
                Assert.IsNotNull(item0);
                Assert.AreEqual("Talisman Saber 2013", item0.Title.Text);
                Assert.AreEqual(0.595, item0.ElementExtensions.ReadElementExtensions<double>("score", "http://a9.com/-/opensearch/extensions/relevance/1.0/")[0], 0.001);

                Assert.IsNull(item0.Summary);

                System.ServiceModel.Syndication.SyndicationItem item1 = listOfSyndicationItems[1];
                Assert.IsNotNull(item1);
                Assert.AreEqual("130717-N-HI414-185\nCORAL SEA (July 17, 2013) The Arleigh Burke-class guided-missile destroyer USS Momsen (DDG 92) fires its MK 45 5-inch lightweight gun during live gunnery exercise in support of Talisman Saber 2013. Talisman Saber 2013 is a biennial training activity aimed at improving Australian and U.S. combat readiness and interoperability. (U.S. Navy photo by Mass Communication Specialist 3rd Class Gregory A. Harden II/Released)", item1.Title.Text);
            }
        }
    }
}